package lifeOfAFish;

import com.sun.opengl.cg.CGcontext;
import com.sun.opengl.cg.CGparameter;
import com.sun.opengl.cg.CGprogram;
import com.sun.opengl.cg.CgGL;

import lifeOfAFish.scenegraph.*;
import lifeOfAFish.scenegraph.geom.*;
/**
 * Class for running a particle system of bubbles 
 * @author Erik Bonner
 *
 */
public class WaterSurface extends Entity {

	private CGcontext cgContext;
	private CGprogram cgVertexProg = null;
	private int cgVertexProfile;
	private CGparameter cgWave; 
	private CGparameter cgCol; 
	private CGparameter cgModelViewProj;
	
	private sgGeomWaterSurface m_waterSurface;
	
	//
	// Constructors
	//
	public WaterSurface () { };
	
	private CGcontext getCgContext() { return cgContext; }


	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void initCg()
	{
		cgContext = CgGL.cgCreateContext();
		cgVertexProfile = CgGL.cgGLGetLatestProfile(CgGL.CG_GL_VERTEX);
		if (cgVertexProfile == CgGL.CG_PROFILE_UNKNOWN)
		{
			System.err.println("Invalid vertex profile");
			System.exit(1);
		}
		CgGL.cgGLSetOptimalOptions(cgVertexProfile);

	}
	
	public CGprogram loadShader(int profile, String filename)
	{
		CGprogram shaderprog = CgGL.cgCreateProgramFromFile(getCgContext(),
				CgGL.CG_SOURCE, filename, profile, null, null);
		if (shaderprog == null)
		{
			int err = CgGL.cgGetError();
			System.err.println("Compile shader [" + filename + "] "
					+ CgGL.cgGetErrorString(err));
			if (CgGL.cgGetLastListing(getCgContext()) != null)
			{
				System.err.println(CgGL.cgGetLastListing(getCgContext()) + "\n");
			}
			System.exit(1);
		}

		CgGL.cgGLLoadProgram(shaderprog);

		int err = CgGL.cgGetError();
		if (err != CgGL.CG_NO_ERROR)
		{
			System.out.println("Load shader [" + filename + "]: "
					+ CgGL.cgGetErrorString(err));
			System.exit(1);
		}

		return shaderprog;
	}
	
	protected void bindParameters()
	{
		cgModelViewProj = CgGL.cgGetNamedParameter(cgVertexProg, "modelViewProj");
		cgWave = CgGL.cgGetNamedParameter(cgVertexProg, "vIn.wave");
		cgCol = CgGL.cgGetNamedParameter(cgVertexProg, "vIn.color");
	}
	
	/**
	 * Initialise the bubbles system
	 */
	public void init() {
		
		// do CG inits
		initCg();
		cgVertexProg = loadShader(cgVertexProfile, "shader/vp_waves.cg");
		bindParameters();
		
		// create the water surface geometry node
		sgTransform trans = new sgTransform();
		trans.translate(.0f, 1.1f, -2.0f);
		
		sgMaterial mat = new sgMaterial();
		mat.setAmbient(0.6f, 0.6f, 0.8f, 0.4f);
		mat.setDiffuse(0.6f, 0.6f, 0.8f, 0.4f);
		mat.setSpecular(0.6f, 0.6f, 0.8f, 0.4f);
		mat.setShininess(100.0f);
		
		
		m_waterSurface = new sgGeomWaterSurface(0.0f, 3.3f, 0.0f, 2.0f);
		m_waterSurface.setCGMod(cgWave);
		m_waterSurface.setCGModelView(cgModelViewProj);
		m_waterSurface.setCGVertexProfile(cgVertexProfile);
		m_waterSurface.setCGVertexProg(cgVertexProg);
		m_waterSurface.setMaterial(mat);
//		m_waterSurface.loadTexture("models/textures/water01.jpg");
		m_waterSurface.setOpaque(false);
		trans.addChild(m_waterSurface);
		
		m_node.addChild(trans);
		
//		trans.addChild(new sgGeomCube(1.0f));
		
	}
	@Override
	public void update() {
		
		

	}
	


}
